// plugin/pages/addressList/addressList.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    app.setNavtitle('地址管理');
  },
  onShow:function(){
    var that = this;
    that.requestAddresslist();
    wx.getStorage({
      key: 'chooseAddr',
      success: function (res) {
        console.log("获取缓存选择地址成功")
        that.setData({
          chooseAddr: res.data
        })
      },
      fail:function(){
        console.log("获取缓存选择地址失败")
      }
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.requestAddresslist();
  },
  requestAddresslist:function(){
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_address_list'
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var addressList = res.data.data;
          var defaultId = '';
          var curAddressData = '';
          for (var i = 0; i < addressList.length; i++) {
            if (addressList[i].isdefault == '1') {
              defaultId = addressList[i].id;
              curAddressData = addressList[i];
              that.setData({
                curAddressData: curAddressData
              })
            }
          }
          that.setData({
            addressList: addressList,
            chooseAddressVal: defaultId
          })
          var chooseType = that.data.chooseDefaultType;
        } else {
          that.setData({
            addressList: [],
            chooseAddressVal: ''
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  chooseAddress:function(e){
    var that = this;
    var chooseAddr = e.currentTarget.dataset.address;
    wx.setStorage({
      key: 'chooseAddr',
      data: chooseAddr,
      success:function(e){
        console.log("缓存选择地址成功")
        that.setData({
          chooseAddr: chooseAddr
        })
        wx.navigateBack({
          delta:1
        })
      }
    })
  },
  back:function(){
    wx.navigateBack({
      delta:1
    })
  },
  editAddress:function(e){
    var dataset = e.currentTarget.dataset;
    var type = dataset.type;
    var id = dataset.id;
    if(type=='edit'){
      var editaddr = dataset.editaddr;
      wx.setStorage({
        key: 'curEditaddr',
        data: editaddr,
        success:function(){
          wx.navigateTo({
            url: '/subpages0/auctionpage/addAddress/addAddress?type=' + type + '&id=' + id
          })
        }
      })
    }else{
      wx.navigateTo({
        url: '/subpages0/auctionpage/addAddress/addAddress?type=' + type + '&id=' + id
      })
    }
  },
  deleteAddress: function (e) {
    var that = this;
    var addressList = that.data.addressList;
    var delId = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '',
      content: '确认删除该收货地址吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          var data = {
            map: 'applet_address_delete',
            id: delId
          }
          console.log(data);
          //发起请求，获取列表列表
          wx.showLoading({
            title: '删除中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                wx.showModal({
                  title: '提示',
                  content: res.data.data.msg,
                  showCancel: false,
                  success: function () {
                    addressList.splice(index, 1);
                    that.setData({
                      addressList: addressList
                    })
                    var chooseAddrId = that.data.chooseAddr && that.data.chooseAddr.id ? that.data.chooseAddr.id:''
                    if (chooseAddrId == delId ){
                      wx.removeStorage({
                        key: 'chooseAddr',
                        success: function (res) {
                          console.log("清除选择地址缓存成功")
                        }
                      })
                    }
                  }
                });
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.em,
                  showCancel: false
                });
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})